package com.syt.cellphone.information.ui.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.ViewGroup
import android.widget.ImageView
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.syt.cellphone.information.R

/**
 *  @author: syt
 *  创建时间: 2020/8/28 9:30
 *  @功能   自定义有中间图标的BottomNavigationView
 *  @version v1.0
 *  定义中的menu文件的中间一个需要放置一个空对象。
 */
class GapBottomNavigationView(context: Context, attrs: AttributeSet?) :
    BottomNavigationView(context, attrs) {

    // 中间图标的资源文件id
    private var iconMiddle = 0

    init {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.GapBottomNavigationView)
        iconMiddle = ta.getResourceId(R.styleable.GapBottomNavigationView_icon_middle, 0)
        ta.recycle()
    }

    @SuppressLint("DrawAllocation", "UseCompatLoadingForDrawables")
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val icon = ImageView(context)
//        icon.maxWidth = 100
//        icon.maxHeight = 100
        val param = LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        // 大小，居中。
        param.gravity = Gravity.CENTER
        param.bottomMargin = 8
        // 加载 img
        icon.setImageDrawable(resources.getDrawable(iconMiddle, null))
        this.addView(icon, param)
        invalidate()
    }
}